package com.gx.gxreader.fragment

import android.content.Intent
import android.support.v7.widget.GridLayoutManager
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.view.View
import com.gx.gxreader.R
import com.gx.gxreader.activity.BookDetailsActivity
import com.gx.gxreader.activity.SearchActivity
import com.gx.gxreader.adapter.CommentAdapter
import com.gx.gxreader.base.BaseFragment
import com.gx.gxreader.presenter.BookshelfPresenter
import com.gx.gxreader.view.BookshelfView
import kotlinx.android.synthetic.main.fragment_bookshelf.*
import kotlinx.android.synthetic.main.rcv_item_bookshelf.view.*


class BookshelfFragment : BaseFragment<BookshelfView, BookshelfPresenter>(), BookshelfView, View.OnClickListener {


    override fun getContentViewId(): Int = R.layout.fragment_bookshelf

    override fun createPresenter(): BookshelfPresenter = BookshelfPresenter()

    override fun initView() {
        val msp = SpannableString("本周已阅读0分钟")
        msp.setSpan(AbsoluteSizeSpan(26, true), 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        tvReadTime.text = msp

        rcvBookShelf.layoutManager = GridLayoutManager(activity, 3)
        rcvBookShelf.adapter = CommentAdapter.Builder()
                .setDatas(getData())
                .setLayoutId(R.layout.rcv_item_bookshelf)
                .bindView(object : CommentAdapter.BindView {
                    override fun onBindView(viewHolder: CommentAdapter.MyViewHolder, data: Any?) {
                        val myData: String = data as String
                        viewHolder.itemView.tvBookName.text = myData
                        if (myData.isEmpty()) {
                            viewHolder.itemView.ivCover.setImageDrawable(activity?.getDrawable(R.drawable.pp_sw_float_btn_open))
                        }
                        viewHolder.itemView.setOnClickListener {
                            if (myData.isEmpty()) {

                            } else {
                                val intent = Intent()
                                intent.setClass(activity, BookDetailsActivity::class.java)
                                startActivity(intent)
                            }
                        }
                    }
                }).create()
    }

    /**
     * 制造假数据
     * 返回一个ArrayList<string> 的集合
     * */
    fun getData(): ArrayList<String> {
        val arrayList = ArrayList<String>()
        val list = listOf<String>("长安十二时辰（上）", "少年江湖物语", "宸汐缘", "")
        //for循环
        for (k in list) {
            arrayList.add(k)
        }
        return arrayList
    }

    override fun initListener() {
        ivSearch.setOnClickListener(this)
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.ivSearch -> {
                val intent = Intent()
                intent.setClass(activity, SearchActivity::class.java)
                startActivity(intent)
            }
        }
    }

}